﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Movies.CopyUtility;
using System.Diagnostics;
using System.Threading;

namespace Movies
{
    public partial class FormShowMovies : Form
    {
        private String source;
        private MovieDetails movieDetails;
        public FormShowMovies(MovieDetails curMovieDetails)
        {
            
            InitializeComponent();
            source = movieDetails.LoadLocation;
            movieDetails = curMovieDetails;
        }

        public FormShowMovies(String sourceFile, Boolean findMode)
        {
            source = sourceFile;
            InitializeComponent();
            if (findMode)
            {
                seenAndPlayButton.Dispose();
            }
        }

        private void FormShowMovies_Load(object sender, EventArgs e)
        {
            try
            {
                Thread t = new Thread(delegate()
                {
                    initiateMovieNameLoadFromThread();
                });
                t.Start();
            }
            catch (Exception ex)
            {
                HelperContentProvider.OmitWarning(ex);
            }
        }

        private void loadMovieNames()
        {
            try
            {
                HelperFileDetailsGuide guide = new HelperFileDetailsGuide(source);
                DTOFileMovementReport report = guide.getReport();

                List<DTOFileMovementDetails> list = report.ValidList;
                String[] extList = HelperDataStoreManager.GetExtList();


                foreach (DTOFileMovementDetails details in list)
                {
                    //Console.WriteLine(details.Name);
                    foreach (String ext in extList)
                    {
                        if (details.IsFile)
                        {
                            if (details.Name.ToLower().EndsWith(ext.ToLower()))
                            {
                                try
                                {
                                    String[] detailTOAdd = new String[2];
                                    detailTOAdd[0] = details.Name;
                                    detailTOAdd[1] = details.BaseFoolder;
                                    int index = MovieNamesGrid.Rows.Add(detailTOAdd);
                                    MovieNamesGrid.Rows.SharedRow(index).Tag = detailTOAdd[1] + detailTOAdd[0];
                                    //clickStart.Visible = true;
                                    if (!seenAndPlayButton.IsDisposed)
                                    {
                                        seenAndPlayButton.Visible = true;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    HelperContentProvider.OmitWarning(ex);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                HelperContentProvider.OmitWarning(ex);
            }
        }

        delegate void InitMovieLoad();
        private void initiateMovieNameLoadFromThread()
        {
            if (this.InvokeRequired)
            {
                    InitMovieLoad d = new InitMovieLoad(initiateMovieNameLoadFromThread);
                    MovieNamesGrid.Invoke(d, new object[] { });
            }
            else
            {
                loadMovieNames();
            }
        }


        private void closeButton_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void MovieNamesGrid_SelectionChanged(object sender, EventArgs e)
        {
            DataGridViewSelectedCellCollection collection = MovieNamesGrid.SelectedCells;
            foreach (DataGridViewCell cell in collection)
            {
                cell.OwningRow.Selected = true;
            }
        }

        private void MovieNamesGrid_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            playMovie();
        }


        private void MovieNamesGrid_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                e.Handled = true;
                playMovie();
            }   
        }



        private void playMovie()
        {
            try
            {
                DataGridViewRow row = MovieNamesGrid.CurrentRow;
                String location = row.Tag.ToString();
                Process.Start(location);
            }
            catch (Exception ex)
            {
                HelperContentProvider.OmitWarning(ex);
            }
        }

        private void playButton_Click(object sender, EventArgs e)
        {
            playMovie();
        }

        private void seenAndPlayButton_Click(object sender, EventArgs e)
        {
            //movieDetails.IsAlreadySeen = true;
            playMovie();
            //this.Dispose();
        }

    }
}
